<?php
namespace Anls\Com\Bar;
use Anls\Common;

/**
 * bar处理
 * @author w.
 *
 */
class BarHandle  {
	
	private $bar; 
	
	/**
	 * 获取bar数据
	 * @param string $content
	 * @param number $beginIndex
	 */
	public function getBarInfo($content,$beginIndex=0){
		$bars = explode("\r\n",$content);
		$bar = array(array());
		// 初始化数据
		// 第一个BAR为0，当前BAR为cnt_bars
		$cntBars = count($bars)-1;
		$i = 0;
		if($beginIndex!=0) $i = $cntBars - $beginIndex;
		// 初始化BAR数据
		for(;$i<=$cntBars;$i++){   
			if(!strpos($bars[$i],","))break;
			$info = explode(",",$bars[$i]);	
			if($info[5] == "0" || $info[6] == "0.00")break;
			$bar[$i]['date'] = $info[0];
			$bar[$i]['dateNum'] = str_replace('/', '', $info[0]);
			$bar[$i]['open'] = $info[1];
			$bar[$i]['high'] = $info[2];
			$bar[$i]['low'] = $info[3];
			$bar[$i]['close'] = $info[4];
			$bar[$i]['vol'] = $info[5];
			$bar[$i]['trunVol'] = $info[6];
			$bar[$i]['index'] = $i;
			if($i!=0){
				if($bar[$i-1]['open']==0){
					$bar[$i]['rise'] = 0;		
				} else {
					$bar[$i]['rise'] = round((($bar[$i]['close']-$bar[$i-1]['close'])/$bar[$i-1]['close'])*100,2);
				}
			} else {
				// 第一个bar
				if($bar[0]['open']==0){
					$bar[0]['rise'] = 0;
				} else {
					$bar[0]['rise'] = round((($bar[0]['close']-$bar[0]['open'])/$bar[0]['open'])*100,2);	
				}
			}
		}	
		$cntBars = $i-1;
		$this->bar = $bar;
		return $bar;
	}
	
	public function getBar(){
		return $this->bar;
	}
	
}

?>
